package com.atomic.test.example.service.impl;


import com.atomic.test.example.entity.Post;
import com.atomic.test.example.entity.Topic;
import com.atomic.test.example.exception.CRUDException;
import com.atomic.test.example.exception.ExceptionCode;
import com.atomic.test.example.query.BaseQuery;
import com.atomic.test.example.query.CreatePostDo;
import com.atomic.test.example.repository.PostRepository;
import com.atomic.test.example.repository.TopicRepository;
import com.atomic.test.example.service.IPostService;
import com.atomic.test.example.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by dreamyao on 2017/3/9.
 */
@Service
public class PostServiceImpl implements IPostService {

    @Autowired
    private PostRepository postRepository;
    @Autowired
    private TopicRepository topicRepository;

    /**
     * 创建帖子，回复帖子
     *
     * @param createPostDo
     * @return
     */
    @Override
    public Integer insertPost(CreatePostDo createPostDo) {

        Post post = new Post();
        Topic queryTopic = topicRepository.findTopicById(createPostDo.getTopicId());
        if (queryTopic == null) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "话题不存在！");
        } else if (!queryTopic.getBoardId().equals(createPostDo.getBoardId())) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "论坛板块不存在！");
        }
        List<Post> posts = postRepository.getPostByPostTypeAndPostTitle(createPostDo.getPostType(),
                createPostDo.getPostTitle());
        if (createPostDo.getPostType() == 1) {
            if (posts.size() != 0) {
                throw new CRUDException(ExceptionCode.DATA_UPDATE_ERROR, "主题帖子已存在！");
            } else {
                post.setBoardId(createPostDo.getBoardId());
                post.setTopicId(createPostDo.getTopicId());
                post.setUserId(UserContext.getLoginUser().getId());
                post.setPostType(createPostDo.getPostType());
                post.setPostTitle(createPostDo.getPostTitle());
                post.setPostText(createPostDo.getPostText());
                post.setCreateTime(new Date());
                postRepository.save(post);
            }
        } else {
            if (posts.size() <= 0) {
                throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "帖子不存在无法进行回复");
            } else {
                post.setBoardId(createPostDo.getBoardId());
                post.setTopicId(createPostDo.getTopicId());
                post.setUserId(UserContext.getLoginUser().getId());
                post.setPostType(createPostDo.getPostType());
                post.setPostTitle(createPostDo.getPostTitle());
                post.setPostText(createPostDo.getPostText());
                post.setCreateTime(new Date());
                postRepository.save(post);

                // 增加话题回复数
                topicRepository.updateTopicReplies(createPostDo.getTopicId(),
                        queryTopic.getTopicReplies() + 1);
            }
        }
        return post.getId();
    }

    /**
     * 查看帖子
     *
     * @param postTitle
     * @return
     */
    @Override
    public List<Post> getPostByPostTitle(String postTitle) {
        List<Post> posts = postRepository.getPostByPostTitle(postTitle);
        if (posts.size() == 0) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "无相关帖子存在！");
        }
        Iterator<Post> iterator = posts.iterator();
        while (iterator.hasNext()) {
            Post post = iterator.next();
            if (post.getPostType() == 1) {
                Topic queryTopic = topicRepository.findTopicById(post.getTopicId());
                topicRepository.updateTopicViews(post.getTopicId(), queryTopic.getTopicViews() + 1);
            }
        }
        return posts;
    }

    /**
     * 根据帖子标题删除帖子
     *
     * @param postTitle 帖子标题
     */
    @Override
    public void deletePostByPostTitle(String postTitle) {
        List<Post> queryPosts = postRepository.getPostByPostTitle(postTitle);
        if (queryPosts == null || queryPosts.size() == 0) {
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "无相关帖子存在！");
        }
        Integer topicId = queryPosts.get(0).getTopicId();
        Topic queryTopic = topicRepository.findTopicById(topicId);
        postRepository.deletePostByPostTitle(postTitle);
        // 去掉话题表中相应的回复数
        topicRepository.updateTopicReplies(queryTopic.getId(),
                queryTopic.getTopicReplies() - queryPosts.size() + 1);
    }

    /**
     * 分页获取所有主题帖子
     * @param baseQuery 分页参数
     * @return
     */
    public LinkedList<Post> getAllPostByPostType(BaseQuery baseQuery){
        return null;
    }
}
